<template>
  <view> </view>
</template>

<script>
import { request, requestData } from '@/utils/http.js'
export default {
  data() {
    return {
      markers: [],
    }
  },
  onShow: function () {
    //  this.getAuthorize()
  },
  onReady() {
    //wx请求获取位置权限
    this.getAuthorize()
      .then(() => {
        //同意后获取
        this.getLocationInfo()
      })
      .catch(() => {
        //不同意给出弹框，再次确认
        this.openConfirm()
          .then(() => {
            this.getLocationInfo()
          })
          .catch(() => {
            this.rejectGetLocation()
          })
      })
  },
  methods: {
    //   初次位置授权
    getAuthorize() {
      return new Promise((resolve, reject) => {
        uni.authorize({
          scope: 'scope.userLocation',
          success: () => {
            resolve() // 允许授权
            this.getLocationInfo()
          },
          fail: () => {
            reject() // 拒绝授权
          },
        })
      })
    },
    // 确认授权后，获取用户位置
    getLocationInfo() {
      const that = this
      uni.getLocation({
        type: 'gcj02',
        success: function (res) {
          // 暂时
          that.longitude = res.longitude //118.787575;
          that.latitude = res.latitude //32.05024;
          console.log('获取当前的用户经度，维度', that.longitude, that.latitude)
          var long = 0
          var lat = 0
          //小数点保留六位  经度
          if (that.longitude.toString().indexOf('.') > 0) {
            const longlatsplit = that.longitude.toString().split('.')
            if (longlatsplit.length >= 2) {
              long =
                parseFloat(longlatsplit[0] === '' ? 0 : longlatsplit[0]) +
                parseFloat('.' + longlatsplit[1].slice(0, 6))
            }
          }
          if (that.latitude.toString().indexOf('.') > 0) {
            const longlatsplit1 = that.latitude.toString().split('.')
            if (longlatsplit1.length >= 2) {
              lat =
                parseFloat(longlatsplit1[0] === '' ? 0 : longlatsplit1[0]) +
                parseFloat('.' + longlatsplit1[1].slice(0, 6))
            }
          }
          uni.setStorageSync('longitude', long)
          uni.setStorageSync('latitude', lat)

          // this.distance(that.latitude,that.longitude);
          that.markers = [
            {
              id: '',
              latitude: res.latitude,
              longitude: res.longitude,
              iconPath: '../../static/img/phone.png',
              width: that.markerHeight, //宽
              height: that.markerHeight, //高
            },
          ]
          that.getList()
          that.openMap(long,lat)
          that.getMapDistance()
        },
      })
    },
    // 拒绝授权后，弹框提示是否手动打开位置授权
    openConfirm() {
      return new Promise((resolve, reject) => {
        uni.showModal({
          title: '请求授权当前位置',
          content: '我们需要获取地理位置信息，为您推荐附近信息',
          success: (res) => {
            if (res.confirm) {
              uni.openSetting().then((res) => {
                if (res[1].authSetting['scope.userLocation'] === true) {
                  resolve() // 打开地图权限设置
                } else {
                  reject()
                }
              })
            } else if (res.cancel) {
              reject()
            }
          },
        })
      })
    },
    // 彻底拒绝位置获取
    rejectGetLocation() {
      uni.showToast({
        title: '你拒绝了授权，无法获得周边信息',
        icon: 'none',
        duration: 2000,
      })
    },
    getList() {
      console.log('获取周边信息')
    },
    openMap(lon, lat) {
      //打开地图，并将门店位置传入
      uni.getLocation({
        success: (res) => {
          // res.latitude=lat;
          // res.longitude=lon;
          //console.log('location success', parseFloat(lat), parseFloat(lon))
          uni.openLocation({
            latitude: parseFloat(lat),
            longitude: parseFloat(lon),
            scale: 18,
          })
        },
      })
    },
    //进行经纬度转换为距离的计算
			Rad(d) {
				return d * Math.PI / 180.0; //经纬度转换成三角函数中度分表形式。
			},

			/*
			 计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
			 默认单位km
			*/
			getMapDistance(lat1, lng1, lat2, lng2) {
        var lat1 = uni.getStorageSync('latitude')  //当前的
        var lng1 = uni.getStorageSync('longitude')
        var lat2 = '39.986186'   //泳池的
        var lng2 = '116.355152'
        console.log('当前位置经纬度：',lat1,lng1);
        console.log('游泳馆经纬度：',lat2,lng2);
   
				var radLat1 = this.Rad(lat1);
				var radLat2 = this.Rad(lat2);
				var a = radLat1 - radLat2;
				var b = this.Rad(lng1) - this.Rad(lng2);
				var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
					Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
				s = s * 6378.137; // EARTH_RADIUS;
				s = Math.round(s * 10000) / 10000; //输出为公里
				//s=s.toFixed(2);
        console.log('相差多少公里：',s);
				return s;
			},

  },
}
</script>

<style>
li {
  list-style: none;
  cursor: pointer;
  width: 300px;
}

.bg {
  background-color: red;
  color: #fff;
}
</style>
